*
* Estimates on total household consumption and income
*
set varabbrev off, permanently
clear all
use epfipc2006-2019plus.dta
global listtotal gasto1-gasto12 year2-year13 ccaa2-ccaa18 income squaredm othehouse ///
       ttenure2-ttenure6 tsize2-tsize5 pdensity2-pdensity3 residshort nmembers nfemales ///
	   nforeign ndworkers n5_15 n16_24 n25_34 n35_64 n65_79 n80omas nstudies2-nstudies4 ///
	   nactive nincome labinc peninc uneinc capinc agemb femalemb foreignmb unemployedmb inactivemb   
*
foreach var1 in T10 T21 T01 T12 {
	preserve
	dis in blue "`var1'"
    keep if post==0 & `var1'!=.
	* propensity score estimation with survey weights
    logit `var1' $listtotal  [pweight=factor]
	predict double ps if e(sample)==1, pr
	drop if ps==.
    gen w=`var1'+((1-`var1')*(ps/(1-ps)))
	* Weights used for ATET estimation accounting for survey weights
	gen estweight=w*factor
	* imposing common support
	quietly sum ps if `var1'==1
    scalar pmintreat=r(min)
	drop if ps<pmintreat & `var1'==0
	quietly sum ps if `var1'==0
    scalar pmaxuntreat=r(max)
	drop if ps>pmaxuntreat & `var1'==1
	/* uncomment this part to get equal estimates with teffects ipw
	drop ps w estweight
	logit `var1' $listtotal  [pweight=factor]
	predict double ps if e(sample)==1, pr
	drop if ps==.
    gen w=`var1'+((1-`var1')*(ps/(1-ps)))
	* Weights used for ATET estimation accounting for survey weights
	gen estweight=w*factor
	*/
    * estimating ATETs	
    gmm (eq1: dgastot - {beta1}*`var1' - {alfa1}) ///
    (eq2: dincome - {beta2}*`var1' - {alfa2}) [pweight=estweight], ///
	instruments(eq1: `var1') instruments(eq2: `var1') winitial(identity) twostep
	nlcom (/beta1)/(/beta2)
	nlcom (/beta1)/(/beta2)-1
	display "Number of treated"
	count if `var1'==1 & e(sample)==1
	display "Number of untreated"
	count if `var1'==0 & e(sample)==1
restore 
}

*
* Estimates on total household consumption and income per adult equivalent
*
clear all
use epfipc2006-2019plus.dta
global listtotalaeq gastoaeq1-gastoaeq12 year2-year13 ccaa2-ccaa18 income squaredm othehouse ///
       ttenure2-ttenure6 tsize2-tsize5 pdensity2-pdensity3 residshort nmembers nfemales ///
	   nforeign ndworkers n5_15 n16_24 n25_34 n35_64 n65_79 n80omas nstudies2-nstudies4 ///
	   nactive nincome labinc peninc uneinc capinc agemb femalemb foreignmb unemployedmb inactivemb
*
foreach var1 in T10 T21 T01 T12 {
	preserve
	dis in blue "`var1'"
    keep if post==0 & `var1'!=.
	* propensity score estimation with survey weights
    logit `var1' $listtotalaeq  [pweight=factor]
	predict double ps if e(sample)==1, pr
	drop if ps==.
    gen w=`var1'+((1-`var1')*(ps/(1-ps)))
	* Weights used for ATET estimation accounting for survey weights
	gen estweight=w*factor
	* imposing common support
	quietly sum ps if `var1'==1
    scalar pmintreat=r(min)
	drop if ps<pmintreat & `var1'==0
	quietly sum ps if `var1'==0
    scalar pmaxuntreat=r(max)
	drop if ps>pmaxuntreat & `var1'==1
    * estimating ATETs	
    gmm (eq1: dgastotaeq - {beta1}*`var1' - {alfa1}) ///
    (eq2: dincomeaeq - {beta2}*`var1' - {alfa2}) [pweight=estweight], ///
	instruments(eq1: `var1') instruments(eq2: `var1') winitial(identity) twostep
	nlcom (/beta1)/(/beta2)
	nlcom (/beta1)/(/beta2)-1
	display "Number of treated"
	count if `var1'==1 & e(sample)==1
	display "Number of untreated"
	count if `var1'==0 & e(sample)==1
restore 
}
*
* The End
*
